/**
 * calculator
 *
 * @author : tzy
 * @Date : 2021-03-16
 * @Description:
 **/

package com.tang.calculator.symbols;

import com.tang.calculator.context.CalculateContext;
import com.tang.calculator.utils.CheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Stack;

@Component
public class MultiplicationSymbol implements OperatorSymbol, Calculation {

    @Autowired
    ValueSymbolFactory valueSymbolFactory;
    @Autowired
    CheckUtils checkUtils;

    public int getPriority() {
        return 50;
    }

    @Override
    public void simpleOperate(CalculateContext context) {
        Stack<ValueSymbol> valueStack = context.getValueStack();
        ValueSymbol value1 = valueStack.pop();
        ValueSymbol value2 = valueStack.pop();

        BigDecimal res = value1.getValue().multiply(value2.getValue());
        ValueSymbol valueSymbol = valueSymbolFactory.create(res.toString());

        valueStack.push(valueSymbol);
    }

    public String getName() {
        return "*";
    }

    /**
     * 校验
     *
     * @param context
     * @return
     */
    @Override
    public String check(CalculateContext context, String str) {
        if (context.getFormula().contains(getName())) {
            return checkUtils.coordinate(context, str, getName());
        }
        return null;
    }
}
